#!/bin/bash

REPOS_URL="https://gitee.com/wx-rdc/secloud-quickstart"

BASE_PATH="$HOME/.docker"

NETWORK_NAME="secloud-network"
MYSQL_DATA="secloud-db-data"
MYSQL_NAME="sec-mysql"
REDIS_NAME="sec-redis"
NACOS_NAME="sec-nacos"

# registry path
REGISTRY="svr01:5000"

SD_NAME="sec-device-security"
SD_VERSION="1.0.0"

MODULES="kun-system kun-auth kun-device kun-service-cipher kun-service-kms kun-gateway"
KUN_VERSION="1.2.0"

SQL_FILES="secloud-ddl.sql secloud-data.sql sd-ddl.sql"

DASHBOARD_SERVICE_NAME="sec-dashboard-service"
DASHBOARD_SERVICE_VERSION="1.0.0"

DASHBOARD_WEBUI_NAME="sec-dashboard-webui"
DASHBOARD_WEBUI_VERSION="1.0.0"

declare -A p=(
    ["kun-system"]=9301
    ["kun-auth"]=9302
    ["kun-device"]=9303
    ["kun-service-cipher"]=9401
    ["kun-service-kms"]=9402
    ["kun-gateway"]=8080
)

create_network() {
  docker network create --driver bridge $NETWORK_NAME
}

create_volume() {
  docker volume create $MYSQL_DATA
}

start_mysql() {
  wget -O "$BASE_PATH/conf/my.cnf" "$REPOS_URL/raw/master/conf/my.cnf"
  docker run -p 3306:3306 -d \
        -e MYSQL_ROOT_PASSWORD=123456 \
        -v $BASE_PATH/conf/my.cnf:/etc/mysql/my.cnf \
        -v $MYSQL_DATA:/var/lib/mysql \
        --restart=on-failure:5 \
        --name $MYSQL_NAME \
        --network $NETWORK_NAME \
        mysql:5.7
        
  sleep 15
}

start_redis() {
  wget -O "$BASE_PATH/conf/redis.conf" "$REPOS_URL/raw/master/conf/redis.conf"
  docker run -p 6379:6379 -d \
        -v $BASE_PATH/conf/redis.conf:/etc/redis/redis.conf \
        --restart=on-failure:5 \
        --name $REDIS_NAME \
        --network $NETWORK_NAME \
        redis:6.0 \
        redis-server /etc/redis/redis.conf
}

start_nacos() {
  wget -O "$BASE_PATH/sql/nacos_config_init01.sql" "$REPOS_URL/raw/master/sql/nacos_config_init01.sql"
  docker exec -i $MYSQL_NAME mysql -uroot -p123456 < $BASE_PATH/sql/nacos_config_init01.sql
  docker run -p 8848:8848 -d \
        -p 9848:9848 \
        -p 9849:9849 \
        -e MODE=standalone \
        -e SPRING_DATASOURCE_PLATFORM=mysql \
        -e MYSQL_SERVICE_HOST=$MYSQL_NAME \
        -e MYSQL_SERVICE_PORT=3306 \
        -e MYSQL_SERVICE_USER=root \
        -e MYSQL_SERVICE_PASSWORD=123456 \
        -e MYSQL_SERVICE_DB_NAME=nacos-config \
        --restart=on-failure:5 \
        --name $NACOS_NAME \
        --network $NETWORK_NAME \
        nacos/nacos-server:v2.1.0

  sleep 10
  wget -O "$BASE_PATH/nacos_config.zip" "$REPOS_URL/raw/master/nacos/nacos_config.zip"
  curl --location --request POST 'http://localhost:8848/nacos/v1/cs/configs?import=true&namespace=public' \
      --form 'policy=OVERWRITE' \
      --form "file=@\"$BASE_PATH/nacos_config.zip\""
}

importSQL() {
  if [ ! -f $BASE_PATH/sql/$1 ]; then
    wget -O "$BASE_PATH/sql/$1" "$REPOS_URL/raw/master/sql/$1"
    docker exec -i $MYSQL_NAME mysql -uroot -p123456 < $BASE_PATH/sql/$1
  fi
}

startSD() {
  PORT="8100"
  [ $1 ] && PORT=$1

  if [ -z `docker ps -q --filter name=$SD_NAME-$PORT` ]; then
    wget -O "$BASE_PATH/sql/sd-data.sql" "$REPOS_URL/raw/master/sql/sd-data.sql"
    docker run -p $PORT:$PORT -d \
        -e MYSQL_HOST=$MYSQL_NAME \
        --restart=on-failure:5 \
        --hostname $SD_NAME-$PORT \
        --name $SD_NAME-$PORT \
        --network $NETWORK_NAME \
        -e CONF_PATH="/app" \
        -e SEC_DEVICE_PORT=$PORT \
        $REGISTRY/$SD_NAME:$SD_VERSION

    cp $BASE_PATH/sql/sd-data.sql .
    sed -i 's/${hostname}/'${SD_NAME}'-'${PORT}'/g' ./sd-data.sql
    sed -i 's/${port}/'${PORT}'/g' ./sd-data.sql
    docker exec -i $MYSQL_NAME mysql -uroot -p123456 < ./sd-data.sql
    rm ./sd-data.sql
  fi
}

startKun() {
  local port="${p[$1]}"
  echo "start $1 on $port..."
  if [ -z `docker ps -q --filter name=$1` ]; then
    docker run -d \
        --name $1 \
        --restart always \
        --network $NETWORK_NAME \
        -p $port:$port \
        $REGISTRY/$1:$KUN_VERSION
    sleep 2s
    docker logs --tail=10 $1
  fi
}

startDashboard() {
  echo "start dashboard service..."
  if [ -z `docker ps -q --filter name=$DASHBOARD_SERVICE_NAME` ]; then
    docker run -p 8000:8000 -d \
        -e MYSQL_HOST=$MYSQL_NAME \
        --restart=on-failure:5 \
        --name $DASHBOARD_SERVICE_NAME \
        --network $NETWORK_NAME \
        -e CONF_PATH="/app" \
        $REGISTRY/$DASHBOARD_SERVICE_NAME:$DASHBOARD_SERVICE_VERSION
    sleep 2s
    docker logs --tail=10 $DASHBOARD_SERVICE_NAME
  fi

  echo "start dashboard webui..."
  if [ -z `docker ps -q --filter name=$DASHBOARD_WEBUI_NAME` ]; then
    docker run -p 9180:80 -d \
        -e MYSQL_HOST=$MYSQL_NAME \
        --restart=on-failure:5 \
        --name $DASHBOARD_WEBUI_NAME \
        --network $NETWORK_NAME \
        $REGISTRY/$DASHBOARD_WEBUI_NAME:$DASHBOARD_WEBUI_VERSION
    sleep 2s
    docker logs --tail=10 $DASHBOARD_WEBUI_NAME
  fi  
}

if [ ! -d $BASE_PATH ]; then
  mkdir -p $BASE_PATH
  mkdir -p $BASE_PATH/conf
  mkdir -p $BASE_PATH/sql
fi

if [ -z $(docker network ls -q --filter name=$NETWORK_NAME) ] ; then
  echo "create network"
  create_network
else
  echo "network already existed"
fi

if [ -z $(docker volume ls -q --filter name=$MYSQL_DATA) ] ; then
  echo "create volume"
  create_volume
else
  echo "volume $MYSQL_DATA already existed"
fi

if [ -z $(docker ps -q --filter name=$MYSQL_NAME) ] ; then
  echo "start mysql"
  start_mysql
else
  echo "mysql already running"
fi

if [ -z $(docker ps -q --filter name=$REDIS_NAME) ] ; then
  echo "start redis"
  start_redis
else
  echo "redis already running"
fi

if [ -z $(docker ps -q --filter name=$NACOS_NAME) ] ; then
  echo "start nacos"
  start_nacos
else
  echo "nacos already running"
fi

for sqlfile in $SQL_FILES; do
  importSQL $sqlfile
done

startSD
startSD 8101

for module in $MODULES; do
  startKun $module
done

startDashboard

exit 0

